unit DBXConnectionTests;

interface

uses
  DBXConnection,
  TestFrameWork;

type
  TDBXConnectionTests = class(TTestCase)
  private

    FDBXConnection: TDBXConnection;
    FAssertFlag: Boolean;

  protected

    procedure SetUp; override;
    procedure TearDown; override;

    procedure AfterConnectionCreate(Sender: TObject);
    procedure BeforeConnectionDestroy(Sender: TObject);

  published

    // Test methods
    procedure TestAfterConstruction;
    procedure TestBeforeDestruction;
    procedure TestAssignParams;
    procedure TestGetPoolStats;
    procedure TestDeletePool;
    procedure TestDeletePools;

  end;

implementation

uses SysUtils, LXResourcePool, DBXEvents, TestData;


{ TDBXConnectionTests }

procedure TDBXConnectionTests.SetUp;
begin
  inherited;
  FDBXConnection := CreateTestConnection;
end;

procedure TDBXConnectionTests.TearDown;
begin
  inherited;
  FreeAndNil(FDBXConnection);
end;

procedure TDBXConnectionTests.AfterConnectionCreate(Sender: TObject);
begin
  FAssertFlag := Sender is TDBXConnection;
end;

procedure TDBXConnectionTests.TestAfterConstruction;
var
  Events: TDBXEvents;
begin
  Events := TDBXEvents.Create(nil);
  try
    Events.OnObjectCreate := AfterConnectionCreate;
    FAssertFlag := False;
    with TDBXConnection.Create(nil) do
      try
        Assert(FAssertFlag);
      finally
        Free;
      end;
  finally
    Events.Free;
  end;
end;

procedure TDBXConnectionTests.BeforeConnectionDestroy(Sender: TObject);
begin
  FAssertFlag := Sender is TDBXConnection;
end;

procedure TDBXConnectionTests.TestBeforeDestruction;
var
  Events: TDBXEvents;
begin
  Events := TDBXEvents.Create(nil);
  try
    Events.OnObjectDestroy := BeforeConnectionDestroy;
    FAssertFlag := False;
    with TDBXConnection.Create(nil) do
      try
        FAssertFlag := False;
      finally
        Free;
      end;
    Assert(FAssertFlag);
  finally
    Events.Free;
  end;
end;

procedure TDBXConnectionTests.TestAssignParams;
var
  Connection: TDBXConnection;
begin
  Connection := TDBXConnection.Create(nil);
  try
    Connection.AssignParams(FDBXConnection);

    Assert(Connection.DriverName = FDBXConnection.DriverName);
    Assert(Connection.LibraryName = FDBXConnection.LibraryName);
    Assert(Connection.GetDriverFunc = FDBXConnection.GetDriverFunc);
    Assert(Connection.VendorLib = FDBXConnection.VendorLib);
    Assert(Connection.Params.Text = FDBXConnection.Params.Text);
    Assert(Connection.PoolOptions.Enabled = FDBXConnection.PoolOptions.Enabled);
    Assert(Connection.PoolOptions.Min = FDBXConnection.PoolOptions.Min);
    Assert(Connection.PoolOptions.Max = FDBXConnection.PoolOptions.Max);
    Assert(Connection.PoolOptions.TimeOut = FDBXConnection.PoolOptions.TimeOut);
    Assert(Connection.PoolOptions.LifeTime = FDBXConnection.PoolOptions.LifeTime);
  finally
    Connection.Free;
  end;
end;

procedure TDBXConnectionTests.TestGetPoolStats;
var
  PoolStats: TLXResourcePoolStats;
begin
  PoolStats := FDBXConnection.GetPoolStats;
  Assert(not PoolStats.Exists);
  FDBXConnection.PoolOptions.Enabled := True;
  FDBXConnection.Open;
  PoolStats := FDBXConnection.GetPoolStats;
  Assert(PoolStats.Exists);
  Assert(PoolStats.BusyCount = 1);
  Assert(PoolStats.IdleCount = 0);
  Assert(PoolStats.PeakCount = 1);
  FDBXConnection.Close;
  PoolStats := FDBXConnection.GetPoolStats;
  Assert(PoolStats.Exists);
  Assert(PoolStats.BusyCount = 0);
  Assert(PoolStats.IdleCount = 1);
  Assert(PoolStats.PeakCount = 1);
  FDBXConnection.DeletePool;
  PoolStats := FDBXConnection.GetPoolStats;
  Assert(not PoolStats.Exists);
end;

procedure TDBXConnectionTests.TestDeletePool;
begin
  FDBXConnection.PoolOptions.Enabled := True;
  FDBXConnection.Open;
  Assert(FDBXConnection.GetPoolStats.Exists);
  FDBXConnection.DeletePool;
  FDBXConnection.Close;
  Assert(not FDBXConnection.GetPoolStats.Exists);
  FDBXConnection.Open;
  Assert(FDBXConnection.GetPoolStats.Exists);
  FDBXConnection.Close;
  FDBXConnection.DeletePool(False);
  Assert(not FDBXConnection.GetPoolStats.Exists);
end;

procedure TDBXConnectionTests.TestDeletePools;
begin
  FDBXConnection.PoolOptions.Enabled := True;
  FDBXConnection.Open;
  Assert(FDBXConnection.GetPoolStats.Exists);
  TDBXConnection.DeletePools;
  FDBXConnection.Close;
  Assert(not FDBXConnection.GetPoolStats.Exists);
  FDBXConnection.Open;
  Assert(FDBXConnection.GetPoolStats.Exists);
  FDBXConnection.Close;
  TDBXConnection.DeletePools(False);
  Assert(not FDBXConnection.GetPoolStats.Exists);
end;

initialization

  TestFramework.RegisterTest('DBXConnectionTests Suite',
    TDBXConnectionTests.Suite);

end.

